package com.inspur.jxpcxt.activitys;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.inspur.jxpcxt.adapter.TeacherCommentsChose;
import com.inspur.jxpcxt.bean.EvaInfo;
import com.inspur.jxpcxt.bean.TeaEvaTeaInfo;
import com.inspur.jxpcxt.bean.TeaInfo;
import com.inspur.jxpcxt.bean.TeacherEvaInfo;
import com.inspur.jxpcxt.service.TeaCommentsService;
import com.inspur.jxpcxt.service.TeaLoginService;
import com.inspur.jxpcxt.utils.JXPCLog;
import com.inspur.jxpcxt.utils.NetworkUtils;
import com.inspur.jypcxt.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
/**
 * 实现教师评测见面
 * @author Administrator
 *
 */
public class TeacherCommentActivity extends FragmentActivity {
	    @ViewInject(R.id.tv_teacher_tno)
		//教师编号
		private TextView mTnoTv = null;
		@ViewInject(R.id.tv_teacher_name)
		//教师姓名
		private TextView mTnameTv = null;
		//
		@ViewInject(R.id.sp_teacher)
		private Spinner mTnamesSp = null;
		//查看信息
		@ViewInject(R.id.tv_look_message)
		private TextView mLookMessageTv = null;
		//
		@ViewInject(R.id.lv_show)
		private ListView mShowLv = null;
		@ViewInject(R.id.tv_corse)
		private TextView mCnameTv = null;
//		@ViewInject(R.id.et_tea_ideal_and_faith)
//		private EditText mIdealEt = null;
//		@ViewInject(R.id.et_tea_moral_sentiments)
//		private TextView mMoralEt = null;
//		@ViewInject(R.id.et_tea_strong_learning)
//		private TextView mLeaningEt = null;
//		@ViewInject(R.id.et_tea_kindheartedness_heart)
//		private TextView mHeartEt = null;
		@ViewInject(R.id.btn_register)
		private Button mRegisterBtn = null;
		List<TeaInfo> teainfoList = new ArrayList<TeaInfo>();
		List<String> tnames = new ArrayList<String>();
		//评论者号
		String tno = null;
		//评论者名字
		String tname = null;
		//被评论者编号
		String obtno = "";
		//被评论者名字
		String obtname = null;
		//课程
		String cname = "";
		
		TeacherEvaInfo evaInfos;
		List<EvaInfo> evaInfoList = new ArrayList<EvaInfo>();
		List<TeaEvaTeaInfo> teaEvalist = new ArrayList<TeaEvaTeaInfo>();
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void onCreate(@Nullable Bundle arg0)
	{
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_teacher_comment);
		ViewUtils.inject(TeacherCommentActivity.this);
		Intent intent = getIntent();
		tno = intent.getStringExtra("tno");
		teainfoList = (ArrayList)intent.getParcelableArrayListExtra("teainfos");
		initData();
		
//		if(!TextUtils.isEmpty(mIdealEt.getText().toString().trim()) && !TextUtils.isEmpty(mMoralEt.getText().toString().trim()) 
//				&& !TextUtils.isEmpty(mLeaningEt.getText().toString().trim()) && !TextUtils.isEmpty(mHeartEt.getText().toString().trim())){
//			//Toast.makeText(TeacherCommentActivity.this, "评分项不可为空", Toast.LENGTH_LONG).show();
//			mRegisterBtn.setEnabled(true);
//		}
	}

private void initData()
	{
		
//		if(TextUtils.isEmpty(mIdealEt.getText().toString().trim()) || TextUtils.isEmpty(mMoralEt.getText().toString().trim()) 
//				|| TextUtils.isEmpty(mLeaningEt.getText().toString().trim()) || TextUtils.isEmpty(mHeartEt.getText().toString().trim())){
//			//Toast.makeText(TeacherCommentActivity.this, "评分项不可为空", Toast.LENGTH_LONG).show();
//			mRegisterBtn.setEnabled(false);
//		}
				//取出该教员的职工号和姓名及分数
		for(int i=0;i<teainfoList.size();i++){
					if(tno.equals(teainfoList.get(i).getTno())){
						mTnameTv.setText(teainfoList.get(i).getTname());
						mTnoTv.setText(teainfoList.get(i).getTno());
						tname = teainfoList.get(i).getTname();
						break;
					}
				}
		for(int i=0;i<teainfoList.size();i++){
			tnames.add(teainfoList.get(i).getTname());
			}
		//创建ArrayAdapter对象
	ArrayAdapter<String> tnameAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item, tnames);
	mTnamesSp.setAdapter(tnameAdapter);
		mTnamesSp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
				{
					// TODO Auto-generated method stub
					//获得点击点击教员所教的课程名
					mCnameTv.setText(teainfoList.get(position).getCname());
					cname = teainfoList.get(position).getCname();
					obtname = teainfoList.get(position).getTname();
					obtno = teainfoList.get(position).getTno();
				}

			@Override
			public void onNothingSelected(AdapterView<?> parent)
				{
					// TODO Auto-generated method stub
					
				}
		});
		
		mShowLv.setAdapter(new TeacherCommentsChose(getApplicationContext()));
		mLookMessageTv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v)
				{
					// TODO Auto-generated method stub
					//跳转到教师详细得分页，把教师评论的分数传过去
					TeaComentDetails();
				}
		});
		mRegisterBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v)
				{
					// TODO Auto-generated method stub
					
				}
		});
	}
//					// TODO Auto-generated method stub
//					if(TextUtils.isEmpty(mIdealEt.getText().toString().trim()) || TextUtils.isEmpty(mMoralEt.getText().toString().trim()) 
//							|| TextUtils.isEmpty(mLeaningEt.getText().toString().trim()) || TextUtils.isEmpty(mHeartEt.getText().toString().trim())){
//						Toast.makeText(TeacherCommentActivity.this, "评分项不可为空", Toast.LENGTH_LONG).show();
//						return;
//					}
//					final float avg;
//					float num0 = Float.parseFloat(mIdealEt.getText().toString().trim());
//					float num1 = Float.parseFloat(mMoralEt.getText().toString().trim());
//					float num2 = Float.parseFloat(mLeaningEt.getText().toString().trim());
//					float num3 = Float.parseFloat(mHeartEt.getText().toString().trim());
//					avg = (num0 + num1 + num2 + num3)/4 ;
//					new Thread(){
//							public void run() {
//									final String result = TeaCommentsService.TeaCommentsByClientGet(obtno, obtname,cname,tno, tname,avg);
//										if(result == null){
//											return;
//										}
//										JXPCLog.e("homeActivity", result);
//										if(result.equals("评测成功")){
//											runOnUiThread(new  Runnable() {
//												public void run()
//													{
//														Toast.makeText(TeacherCommentActivity.this,result , Toast.LENGTH_LONG).show();
//													}
//											});
//										}else{
//											runOnUiThread(new  Runnable() {
//												public void run()
//													{
//														Toast.makeText(TeacherCommentActivity.this,"请求失败……" , Toast.LENGTH_LONG).show();
//													}
//											});
//										}
//							};
//					}.start();
//				}
//		});
//		}
private void TeaComentDetails()
	{
		if (!NetworkUtils.isConnectInternet(this)) {
			Toast.makeText(TeacherCommentActivity.this, "网络不可用，请检查网络连接",
					Toast.LENGTH_SHORT).show();
			return;
		}
		// TODO Auto-generated method stub

		// 子线程访问网络
		new AsyncTask<Void, Void, String>() {

			@Override
			protected String doInBackground(Void... params)
				{
					// TODO Auto-generated method stub
					String result = TeaLoginService.TeaLoginByClientGet(
							tno);
					JXPCLog.e("teacomments", result.toString());
					return result;
				}

			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			protected void onPostExecute(String result)
				{
//					if(result.equals("登录失败")){
//						Toast.makeText(TeacherCommentActivity.this, "请核实你的密码和账号！", Toast.LENGTH_LONG).show();
//						return;
//					}
					if( result == null ||result.length() == 0){
						Toast.makeText(TeacherCommentActivity.this, "请求失败……", Toast.LENGTH_LONG).show();
						return;
					}
					// 解析数据
					Gson gson = new Gson();
					evaInfos = gson.fromJson(result,
							new TypeToken<TeacherEvaInfo>() {
							}.getType());
					//ArrayList<EvaInfo> arrayList = new ArrayList<EvaInfo>();
					evaInfoList =  evaInfos
							.getTeaevainfos();
					
					teaEvalist = evaInfos.getTeaevateainfo();
					if (evaInfoList == null || teaEvalist == null) {
						return;
					}
//					 for (EvaInfo ei : arrayList) {
//					 JXPCLog.e("sssss", ei.toString());
//					 }
					Bundle data = new Bundle();
					data.putParcelableArrayList("evaInfoList", (ArrayList)evaInfoList);
					data.putParcelableArrayList("teaevaList", (ArrayList)teaEvalist);
					data.putString("tno", tno);
//					Intent intent = new Intent(LoginActivity.this,
//							TeacherActivity.class);
					Intent intent = new Intent();
					intent.setDataAndType(Uri.parse("xds://www.xiangdeshan.com:8888/xds"),"abc/test");
					intent.putExtras(data);
					JXPCLog.e("loginteacher");
					startActivity(intent);
					//TeacherCommentActivity.this.finish();
				}
		}.execute();

				}
}
